package com.game.utils;

import java.io.UnsupportedEncodingException;
import java.util.Base64;

public class SecStringUtil {
	

	public static String secString(String str) throws UnsupportedEncodingException{
		
		byte[] bytes = str.getBytes("UTF-8");
		for (int i = 0; i < bytes.length; i++) {
			//加密,客户端使用同样方式解密
			byte v = (byte) (bytes[i]^(i%Byte.MAX_VALUE));
			bytes[i] = v;
		}
		str = Base64.getEncoder().encodeToString(bytes);
		return str;
	}
	public static String unSecString(String str) throws UnsupportedEncodingException{
		byte[] bytes= Base64.getDecoder().decode(str);
		for (int i = 0; i < bytes.length; i++) {
			//加密,客户端使用同样方式解密
			bytes[i] = (byte) (bytes[i]^(i%Byte.MAX_VALUE));
		}
		return new String(bytes,"UTF-8");
	}
	public static String parseSecString(String str) throws UnsupportedEncodingException{
		byte[] bytes = Base64.getDecoder().decode(str);
		for (int i = 0; i < bytes.length; i++) {
			bytes[i] = (byte) (bytes[i]^(i%Byte.MAX_VALUE));
		}
		return new String(bytes, "UTF-8");
	}
	public static void main(String[] args) {
		try {
			String str = secString("债市");
			System.out.println(str);
			String str2 = unSecString(str);
			System.out.println(str2);
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
				
	}
}
